package backtrack;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author : Rookie
 * @description : 递增子序列
 * @date : 2023/12/7
 */
public class IncreasingSubsequence {

    public static void main(String[] args) {
        int[] nums = new int[] {4,7,6,7};
        List<List<Integer>> result = new ArrayList<>();
        backtrack(nums,0,new ArrayList<>(),result);
        System.out.println(result);
    }

    private static void backtrack(int[] nums, int startIndex, List<Integer> path,List<List<Integer>> result) {
        if (path.size() > 1) {
            result.add(new ArrayList<>(path));
        }

        Set<Integer> usedNum = new HashSet<>();
        for (int i = startIndex; i < nums.length; i++) {
            // 如果已被使用 跳出本次循环
            if (usedNum.contains(nums[i])) {
                continue;
            }
            if (path.isEmpty() || nums[i] >= path.get(path.size()-1)) {
                path.add(nums[i]);
                usedNum.add(nums[i]);
                backtrack(nums,i+1,path,result);
                path.remove(path.size()-1);
            }
        }
    }
}
